# 导入蓝图的包
from flask import Blueprint,jsonify,request
import json
from db import db
# 创建蓝图
bp_test = Blueprint('test',__name__)

@bp_test.route('/stream',methods=['GET','POST','PUT','DELETE'])
def stream():
    if request.method=='POST':
        # 获取数据
        data = request.data
        # 如果获取到了
        if data:
            # 转成json格式
            data = json.loads(data)
        # data没获取到就永form获取
        else:
            # form获取不用转json格式
            data = request.form
        # 构造sql语句
        sql = """insert into stream values(0,'%s','%s')"""%(data['name'],"")
        # 添加
        db.insert(sql)
        return jsonify({'code':200})

    elif request.method=='GET':
        # 获取stream表中的所有数据
        sql = """select * from stream"""
        # 获取所有
        res = db.find_all(sql)
        # 返回
        return jsonify({'code':200,'list':res})
